/*
This do-file cleans the Miami courts data originally used in "The effects of pre-trial detention on conviction, future crime, and employment: Evidence from randomly assigned judges", by Dobbie, Goldin, and Yang (2018) and in the empirical example in Chyn, Frandsen, and Leslie's guide to using examiner leniency designs.
*/



********************************************
* If you don't want to run the code from the master do-file (JFEguide_masterscript), update the root directory filepath global here. If you run the code from the master do-file, it will control the root directory filepath and ignore the filepath below.

if "$master" == ""	{
****************************
*** UPDATE FILEPATH HERE ***
****************************
global root_dir /Users/andersee/Dropbox/cluster_jackknife/restat/cjive_replication_package
*****************************

*** subfolders
global raw_data  $root_dir/raw_data
global intermediate_data $root_dir/intermediate_data
global clean_data $root_dir/clean_data
global tabfig $root_dir/tabfig
global logfiles $root_dir/logfiles
}
********************************************


clear
set more off
cap log close

log using $logfiles/clean_Miami_data.log, replace


local start_time_clean = clock(c(current_date) + " " + c(current_time), "DMYhms")
display "Start time clean: " %tcCCYY-NN-DD_HH:MM:SS `start_time_clean'



*** Read in each year's raw data, save in temp files, append into a single file, erase temp files
clear
foreach year of numlist 6/16 {
	clear
	insheet using "$raw_data/YR`year'_74103.TXT", delimiter("^") 
	tostring v12, replace
	save "$intermediate_data/temp`year'.dta", replace
}

clear
use $intermediate_data/temp6 
foreach year of numlist 7/16 {
	append using "$intermediate_data/temp`year'.dta"
}

foreach year of numlist 6/16 {
	capture erase "$intermediate_data/temp`year'.dta"
}

*** Add variable names
rename v1 casenumber
rename v2 lastname
rename v3 firstname
rename v4 middlename
rename v5 dob
rename v6 race
rename v7 gender
rename v8 addr_street
rename v9 addr_apt
rename v10 addr_city
rename v11 statecode
rename v12 postal

rename v13 baildate
rename v14 bailpostdate
rename v15 bailjudge
rename v16 bail_amount
rename v17 bailtype
rename v18 bailmet

rename v19 defenseattorney
rename v20 prosecutor
rename v21 trialdate
rename v22 judicialsection
rename v23 trialjudge

rename v24 arrestdate
rename v25 arrest_offensetype
rename v26 arrest_offensedegree
rename v27 arrest_offensestatute
rename v28 arrest_offensedescription

rename v29 filedate
rename v30 file_offensetype
rename v31 file_offensedegree
rename v32 file_offensestatute
rename v33 file_offensedescription

rename v34 casedispositiondate
rename v35 disposition_offensetype
rename v36 disposition_offensedegree
rename v37 disposition_offensestatute
rename v38 disposition_offensedescription

rename v39 offensedisposition
rename v40 sentencetype
rename v41 sentencelength
drop v42 v43


*** drop transferred/consolidated cases
gen transfer = strpos(offensedisposition, "CONSOLIDATE") | offensedisposition=="TRANSF TO OTHER CASE" | offensedisposition=="TRANSFER TO CIRC CRT" | offensedisposition=="TRANSFER-COUNTY  CRT"
drop if transfer==1



** clean dates, including dates of birth
foreach var of varlist baildate bailpostdate trialdate arrestdate filedate casedispositiondate {
	replace `var' = . if `var'==0
}

foreach var in bail bailpost arrest file casedisposition {
	tostring `var'date, replace

	gen `var'year = substr(`var'date,1,4)
	gen `var'month = substr(`var'date,5,2)
	gen `var'day = substr(`var'date,7,2)
	destring `var'year `var'month `var'day, replace

	gen clean_`var'date = mdy(`var'month, `var'day, `var'year )
	format clean_`var'date %d
	}
	

replace dob = . if dob==9010101
replace dob = . if dob==0

tostring dob, replace
gen birthyear = substr(dob,1,4)
destring birthyear, replace
gen age = bailyear -birthyear
replace age = 0 if age==.
replace age = 70 if age>70
gen missing_age = (age==0)

replace dob = "" if dob=="."


* drop juveniles
drop if age<18 & missing_age==0



**** impute missing bail dates as best we can
replace clean_baildate = clean_bailpostdate if (clean_arrestdate - clean_bailpostdate<=3 & clean_baildate==. & clean_bailpostdate!=. & bailmet=="Y")
replace clean_baildate = clean_arrestdate + 1 if (clean_baildate==. & bailmet=="N")
	
	
*** construct bail DOW and week vars
gen baildow =  dow(clean_baildate)
gen bailweek =  week(clean_baildate)



*** cleaning demographics
gen male = (gender=="M")
gen white = (race=="W")
gen black = (race=="B")

gen missing_race = (race=="U" | race=="")
gen missing_gender = (gender=="U" | gender=="")




*** Drop county ordinance violations
//there are a bunch of panhandling or little violations that do not qualify for bail 
drop if arrest_offensetype!="F" & arrest_offensetype!="M"



*** create indicators for each crime x outcome. we will calculate totals during the collapse
gen n_counts = 1

gen arrest_offensegrade = arrest_offensetype + arrest_offensedegree
gen file_offensegrade = file_offensetype + file_offensedegree
gen disp_offensegrade = disposition_offensetype + disposition_offensedegree


gen n_acrime_f1 = arrest_offensegrade=="F1" | arrest_offensegrade=="FL" | arrest_offensegrade=="FC"
gen n_acrime_f2 = arrest_offensegrade=="F2"
gen n_acrime_f3 = arrest_offensegrade=="F3"
gen n_acrime_m1 = arrest_offensegrade=="M1"
gen n_acrime_m2 = arrest_offensegrade=="M2"

gen n_dcrime_f1 = disp_offensegrade=="F1" | disp_offensegrade=="FC" | disp_offensegrade=="FL"
gen n_dcrime_f2 = disp_offensegrade=="F2"
gen n_dcrime_f3 = disp_offensegrade=="F3"
gen n_dcrime_m1 = disp_offensegrade=="M1"
gen n_dcrime_m2 = disp_offensegrade=="M2"

egen tmp = rowtotal(n_acrime*)
gen n_crime_other = tmp==0
drop tmp

gen n_assault = (substr(arrest_offensedescription,1,7)=="ASSAULT" | substr(arrest_offensedescription,1,7)=="BATTERY")
gen n_burglary = substr(arrest_offensedescription,1,8)=="BURGLARY"
gen n_drug = substr(arrest_offensedescription,1,11)=="AMPHETAMINE" | substr(arrest_offensedescription,1,8)=="CANNABIS" | substr(arrest_offensedescription,1,7)=="COCAINE" | substr(arrest_offensedescription,1,8)=="CONT SUB" | substr(arrest_offensedescription,1,14)=="CONTROLLED SUB" | substr(arrest_offensedescription,1,6)=="HEROIN" | substr(arrest_offensedescription,1,13)=="ILLEGAL DRUGS" | substr(arrest_offensedescription,1,9)=="MARIJUANA" | substr(arrest_offensedescription,1,8)=="NARCOTIC" | substr(arrest_offensedescription,1,14)=="PHENETHYLAMINE" | substr(arrest_offensedescription,1,6)=="PRESCR" | regexm(arrest_offensedescription,"DRUG")
gen n_attmurder = substr(arrest_offensedescription,1,23)=="ATTEMPTED FELONY MURDER"
gen n_arson = substr(arrest_offensedescription,1,5)=="ARSON" | substr(arrest_offensedescription,1,8)=="PROPERTY"
gen n_weapon = substr(arrest_offensedescription,1,6)=="WEAPON" | substr(arrest_offensedescription,1,9)=="CONCEALED" | substr(arrest_offensedescription,1,7)=="FIREARM"
gen n_fraud = substr(arrest_offensedescription,1,9)=="WORTHLESS" | substr(arrest_offensedescription,1,8)=="UTTERING" | substr(arrest_offensedescription,1,11)=="CREDIT CARD" | substr(arrest_offensedescription,1,4)=="FORG" | substr(arrest_offensedescription,1,14)=="IDENTIFICATION" | substr(arrest_offensedescription,1,9)=="INSURANCE" | substr(arrest_offensedescription,1,15)=="ORGANIZED FRAUD"
gen n_dui = substr(arrest_offensedescription,1,13)=="DRIVING UNDER" | substr(arrest_offensedescription,1,3)=="DUI"
gen n_mischief = substr(arrest_offensedescription,1,17)=="CRIMINAL MISCHIEF"
gen n_murder = substr(arrest_offensedescription,1,6)=="MURDER" | substr(arrest_offensedescription,1,10)=="FEL MURDER" | substr(arrest_offensedescription,1,12)=="MANSLAUGHTER"
gen n_theft = substr(arrest_offensedescription,1,12)=="PETIT RETAIL" | substr(arrest_offensedescription,1,5)=="THEFT" | substr(arrest_offensedescription,1,11)=="GRAND THEFT" | substr(arrest_offensedescription,1,13)=="PETIT LARCENY"| substr(arrest_offensedescription,1,11)=="PETIT THEFT" | substr(arrest_offensedescription,1,12)=="RETAIL THEFT" | substr(arrest_offensedescription,1,15)=="STOLEN PROPERTY"
gen n_kidnap = substr(arrest_offensedescription,1,10)=="KIDNAPPING"
gen n_trespass = substr(arrest_offensedescription,1,8)=="TRESPASS"
gen n_rape = substr(arrest_offensedescription,1,14)=="SEXUAL BATTERY"
gen n_robbery= substr(arrest_offensedescription,1,7)=="ROBBERY"


gen n_guilty = substr(offensedisposition,1,4)=="CONV" | substr(offensedisposition,1,6)=="WH ADJ" | substr(offensedisposition,1,5)=="ADJ W"
gen n_conv = substr(offensedisposition,1,4)=="CONV"
gen n_dismiss = (substr(offensedisposition,1,4)=="DISM")
gen n_noaction = substr(offensedisposition,1,9)=="NO ACTION"
gen n_nolle = (substr(offensedisposition,1,5)=="NOLLE")
gen n_notguilty = substr(offensedisposition,1,6)=="ACQUIT"
gen n_withdraw = (offensedisposition=="VOLUNTARILY WTHDRAWN" | substr(offensedisposition,1,9)=="WITHDRAWN")
gen n_timeserv = offensedisposition=="CONV W/CTS" | offensedisposition=="CONV/CTS/FINE/COST" | offensedisposition=="WH ADJ / CTS" | offensedisposition=="WH ADJ REHAB/CRT CST"|offensedisposition=="ADJ WH/CTS/FINE/COST"
gen n_adjwh = substr(offensedisposition,1,6)=="WH ADJ" | substr(offensedisposition,1,5)=="ADJ W"
gen n_transfer = substr(offensedisposition,1,6)=="TRANSF"

** major crime types: property, drug, violent
gen n_property = (n_theft>0 | n_burglary>0 | n_robbery>0 | n_fraud>0 | n_arson>0 | n_trespass>0)
gen n_violent = (n_assault>0 | n_attmurder>0 | n_murder>0 |n_rape>0 | n_kidnap>0)


* crime type of highest charge
* first create numeric version of the charge variable that is defined only if you are in our 4 categories
gen tmp = 1 if (n_dui==1 | n_drug==1 | n_property==1 | n_violent==1)
replace tmp = 2 if n_acrime_m2==1 & (n_dui==1 | n_drug==1 | n_property==1 | n_violent==1)
replace tmp = 3 if n_acrime_m1==1 & (n_dui==1 | n_drug==1 | n_property==1 | n_violent==1)
replace tmp = 4 if n_acrime_f3==1 & (n_dui==1 | n_drug==1 | n_property==1 | n_violent==1)
replace tmp = 5 if n_acrime_f2==1 & (n_dui==1 | n_drug==1 | n_property==1 | n_violent==1)
replace tmp = 6 if n_acrime_f1==1 & (n_dui==1 | n_drug==1 | n_property==1 | n_violent==1)

* second find highest charge by casenumber
bys casenumber: egen tmp2 = max(tmp)

* now save a number representing the crime type for highest charge
gen tmp3 = .
replace tmp3 = 1 if tmp2==tmp & n_dui==1
replace tmp3 = 2 if tmp2==tmp & n_drug==1
replace tmp3 = 3 if tmp2==tmp & n_property==1
replace tmp3 = 4 if tmp2==tmp & n_violent==1

* break ties and copy answer to all rows for that otn
bys casenumber: egen tmp4 = max(tmp3)
gen highest_crimetype = "none categorized"
replace highest_crimetype = "dui" if tmp4==1
replace highest_crimetype = "drug" if tmp4==2
replace highest_crimetype = "property" if tmp4==3
replace highest_crimetype = "violent" if tmp4==4


* option to have n_ in each crime type x outcome 
foreach crimetype in dcrime_m2 dcrime_m1 dcrime_f3 dcrime_f2 dcrime_f1 {
	gen n_guilty_`crimetype' = 0
	replace n_guilty_`crimetype' = n_`crimetype' if n_guilty==1

}


*** Prep bail vars
* put bail in $1000s
replace bail_amount = bail_amount/1000

gen bail_met_ever = bailmet=="Y"

* indicator for posting bail on exact date bail is determined
gen bail_met_0days = bail_met_ever==1 & clean_baildate == clean_bailpostdate

* indicator for posting bail within 2 weeks 
gen clean_timetobail = clean_bailpostdate - clean_baildate
replace clean_timetobail = clean_casedispositiondate - clean_baildate if clean_bailpostdate==.
replace clean_timetobail = 365 if clean_timetobail > 365 & clean_timetobail!=.

*** Virtually all missings are bail_met==0 because these cases are almost all no actions or nolle prosequi ***
replace clean_timetobail = 0 if clean_timetobail ==.


gen bail_met_14days =  bail_met_ever==1  & clean_timetobail <=14

* Main treatment var (indicator for posting bail within 3 days)
gen bail_met =  bail_met_ever==1  & clean_timetobail <=3


gen bail_ror = bailtype=="RELEASE ON OWN RECOG" & bailtype!="UNKNOWN RELEASE CODE"

gen bail_mon = (bailtype=="SURETY BOND") | (bailtype=="CASH BOND") | (bail_met_ever==0 & bail_amount>0)
replace  bail_mon = 1 if bail_amount==0 & bail_met_ever==0 & (clean_baildate==clean_casedispositiondate)

gen bail_nonmon = (bail_ror==0 & bail_mon==0)

gen bail_release = (bail_ror==1|bail_nonmon==1)




* sentence length 
gen days_prison = sentencelength if sentencelength==0
replace days_prison = sentencelength if sentencetype=="DAYS"
replace days_prison = sentencelength/24 if sentencetype=="HOURS"
replace days_prison = sentencelength*30 if sentencetype=="MONTHS"
replace days_prison = sentencelength*365 if sentencetype=="YEARS"
replace days_prison = 200000 if sentencetype=="LIFE"

* topcoding at 10 years
replace days_prison = 365*10 if days_prison > 365*10 & days_prison!=.



* indicator for having a public defender
gen attorney_public = (defenseattorney=="PUBLIC, ASSIGNED" | defenseattorney=="PUBLIC, ASSIGNED MISD")



*** time to case disposition
gen days_to_disposition = clean_casedispositiondate - clean_baildate
replace days_to_disposition = . if days_to_disposition<0



*** collapse to get crime charge counts
sort casenumber clean_baildate clean_casedispositiondate

collapse  (sum) n_* bail_amount (max) clean_casedispositiondate days_to_disposition days_prison  bail_met_ever bail_met_0days bail_met_14days bail_met bail_mon bail_nonmon bail_ror attorney_public (last) clean_baildate lastname firstname middlename dob race age male white black gender addr*  statecode postal bailjudge bailtype  baildate bailpostdate bailyear bailmonth bailday baildow bailweek defenseattorney prosecutor trialdate judicialsection trialjudge arrest* file* clean_arrestdate clean_filedate  disposition* offensedisposition sentence* highest_crimetype missing* clean_timetobail , by(casenumber)
 



*** construct indicator vars for case outcomes and charges
* guilt variables
foreach var in guilty conv dismiss noaction nolle notguilty withdraw  adjwh {
	gen any_`var' = n_`var'>0 & n_`var'!=.
}

* charge variables
foreach var in guilty_dcrime_f1 guilty_dcrime_f2 guilty_dcrime_f3  guilty_dcrime_m1 guilty_dcrime_m2 acrime_f1 acrime_f2 acrime_f3 acrime_m1 acrime_m2 crime_other assault burglary drug attmurder arson weapon fraud dui mischief murder theft kidnap trespass rape robbery violent property {
	gen any_`var' = n_`var'>0 & n_`var'!=.
}



gen highest_charge = 1
replace highest_charge = 2 if any_acrime_m2==1 
replace highest_charge = 3 if any_acrime_m1==1 
replace highest_charge = 4 if any_acrime_f3==1 
replace highest_charge = 5 if any_acrime_f2==1 
replace highest_charge = 6 if any_acrime_f1==1 

gen highest_charge_felony = highest_charge>=4
gen highest_charge_misd = highest_charge<=3


// note that we leave highest guilty = 0 if you are not convicted
gen highest_guilty = 0
replace highest_guilty = 1 if any_guilty==1 
replace highest_guilty = 2 if any_guilty_dcrime_m2==1 
replace highest_guilty = 3 if any_guilty_dcrime_m1==1 
replace highest_guilty = 4 if any_guilty_dcrime_f3==1 
replace highest_guilty = 5 if any_guilty_dcrime_f2==1 
replace highest_guilty = 6 if any_guilty_dcrime_f1==1 


gen highest_guilty_felony = highest_guilty>=4
gen highest_guilty_misd   = highest_guilty>=1 & highest_guilty<=3


* indicator for highest guilty < highest charge
// note that charge_down = 1 if you are not convicted
gen charge_down = 0
replace charge_down = 1 if highest_guilty<highest_charge & any_guilty==1 
replace charge_down = 1 if any_guilty==0

gen case_plea = ( any_guilty>0 & trialdate==.)
gen case_conv_plea = ( any_conv>0 & trialdate==.)

gen confinement = days_prison>0 & any_conv==1

gen any_timeserv = n_timeserv>0 & n_timeserv!=. & confinement==0



** indicator for case pending
gen case_pending = (substr(offensedisposition,1,9)=="NO ACTION" & n_counts==n_noaction) | offensedisposition==""


* change outcomes to 0 if case still pending
foreach var of varlist case_plea case_conv_plea any_guilty n_guilty any_conv n_conv any_timeserv charge_down confinement {
	replace `var' = 0 if case_pending==1
}



* top coding counts and no action
replace n_counts = 10 if n_counts>10
replace n_noaction = 10 if n_noaction>10


*** create individual ids
gen lastfour=substr(lastname,1,4)
gen firstthree=substr(firstname,1,3)
sort dob lastfour firstthree gender 
egen id = group(dob lastfour firstthree gender missing_gender)



*** create indicators for recidivism and having a prior offense
sort id clean_baildate clean_arrestdate clean_casedispositiondate
by id: gen case_order = _n


sort id clean_baildate
bys id clean_baildate: gen index = _n
bys id: egen total_cases = max(case_order)

gen recid_ever = (case_order < total_cases)
gen prior_offender_ever = case_order>=2



* court variable (felony vs misdemeanor)
gen court = substr(casenumber,1,1)
replace court="M" if court=="B"
gen felony_court = court=="F"

/* REMOVE?
*** Sample restrictions
keep if bailyear>=2006 & bailyear<=2015

* Most DUI cases not included in the data, so we drop the small number that we have
drop if any_dui==1 & (court=="F" | court=="M")


* create judge_sample indicator (but keep those not in judge sample too)
gen judge_sample = 1

replace judge_sample = 0 if bailyear>=2015

replace judge_sample = 0 if bailjudge==""

replace judge_sample = 0 if substr(bailjudge,1,8)=="ASSIGNED"

* sample restriction of being on weekend 
replace judge_sample = 0 if  baildow!=0 & baildow!=6 

* keep only if meet all sample criteria based on timing of case
keep if judge_sample==1
*/


*** Sample restrictions
keep if inrange(bailyear,2006,2014)

	* Most DUI cases not included in the data, so we drop the small number that we have
drop if any_dui==1 & (court=="F" | court=="M")

drop if bailjudge=="" | substr(bailjudge,1,8)=="ASSIGNED"

drop if baildow!=0 & baildow!=6 


* clean up judge names
replace bailjudge = "TINKLER, MARISA" if bailjudge=="TINKLER-MENDEZ, MARISA"
replace bailjudge = "LEHR, MIRIAM" if bailjudge=="LEHR, MYRIAM"
replace bailjudge = "FRUSCIANTE, JOHN" if bailjudge=="FRUCIANTE, JOHN"
replace bailjudge = "GONZALEZ-PAULSON, MICHAELLE" if bailjudge=="GONZALEZ-PAULSON, M"
replace bailjudge = "MANNO, VALERIE" if bailjudge=="MANNO-SCHURR, VALERIE"
 


* Calculate number of cases per judge (sample restriction based on cases/judge in analysis file)
bys bailjudge: egen n_cases = count(n_counts)
bys bailjudge bailyear: egen n_cases_year = count(n_counts)




* make numeric judge id and judge + judge/year indicators
egen judgeid = group(bailjudge)
xi, pre(jfe_) i.judgeid

egen jyearid = group(bailjudge bailyear)
xi, pre(jyf_) i.jyearid


* make shift indicators (for clustering)
egen shift = group(court clean_baildate)


* vars for FE
egen year_x_dow_x_court = group(court bailyear baildow)
egen month_x_dow_x_court = group(court bailmonth baildow)

xi, pre(fe_) noomit i.year_x_dow_x_court i.month_x_dow_x_court






*** Clean defendant names and make individual ID
replace lastname = strupper(lastname)
replace firstname = strupper(firstname)
split(firstname),limit(1) generate(first_new) //if first name has spaces, only keep before the space
drop firstname
rename first_new firstname
split(lastname),limit(1) generate(last_new) //if last name has spaces, only keep before the space
drop lastname
rename last_new lastname
split(lastname),limit(1) p(,) generate(last_new) //if last name has ",", only keep before the comma
drop lastname
rename last_new lastname

//O'Brien shows up like 'O''Brien'. Or D'angelo
gen flag=1 if strpos(lastname,"'")>0
split(lastname) if flag==1,limit(2) p(') generate(temp) //'O and BRIEN'
split(temp2) if flag==1,limit(1) p(') generate(tempB) //changes Brien' to Brien
split(temp1) if flag==1,limit(2) p(') generate(tempA) //"" and O
egen temp5=concat(temp1 temp2) if flag==1 //Brien to OBrien
replace lastname = temp5 if flag==1
drop temp* flag


drop lastfour firstthree id
gen lastfour=substr(lastname,1,4)
gen firstthree=substr(firstname,1,3)


* replace missing gender if consistent within id
gen tmp = (male==1) if missing_gender==0

bys dob lastfour firstthree: egen min_tmp = min(tmp)
bys dob lastfour firstthree: egen max_tmp = max(tmp)
bys dob lastfour firstthree: gen same = min_tmp==max_tmp

replace gender="M" if same==1 & min_tmp==1 & max_tmp==1 & missing_gender==1
replace gender="F" if same==1 & min_tmp==0 & max_tmp==0 & missing_gender==1
drop min_tmp max_tmp same tmp


replace gender = substr(gender,1,1)
replace gender = "U" if gender==""
drop missing_gender
gen missing_gender = gender=="U"
drop male
gen male = gender=="M"


* replace variables = . if missing
replace white = . if missing_race==1
replace black = . if missing_race==1
replace male = . if missing_gender==1
gen female = .
	replace female = 1 if male==0
	replace female = 0 if male==1


sort dob lastfour firstthree gender 
egen id = group(dob lastfour firstthree gender missing_gender)


* make rescaled versions of variables for balance table (so we don't get things like 0.000 (0.000))
foreach var in male black age prior_offender_ever n_counts highest_charge_felony any_drug any_violent any_property	{
	
	gen `var'100 = `var'/100
}


*** variable labels
label var white "White"
label var black "Black"
label var male "Male"
label var age "Age at Bail Decision"
label var n_counts "Number of Offenses" 
label var highest_charge_felony "Felony Offense"
label var any_drug "Any Drug Offense"
label var any_dui "Any DUI Offense"
label var any_violent "Any Violent Offense"
label var any_property "Any Property Offense"
label var missing_race "Missing Race"
label var missing_gender "Missing Gender"
label var missing_age "Missing Age"
label var case_plea "Guilty Plea"
label var any_timeserv "Guilty Plea to Time Served"
label var any_guilty "Any Guilty Offense"
label var any_conv "Any Conviction"
label var n_guilty "Number of Guilty Offenses"
label var charge_down "Offense Charged Down"
label var confinement "Any Incarceration"
label var bail_amount "Bail Amount (in thousands)"
label var bail_ror "Release on Recognizance"
label var bail_nonmon "Non-Monetary Bail "
label var bail_mon "Monetary Bail"
label var days_to_disposition "Days to Case Decision"
label var days_prison "Max Days Incarcerated"
label var attorney_public "Public Defender" 
label var highest_charge_misd "Misdemeanor Only"
label var bail_met_14days "Released in 14 Days"
label var bail_met_ever "Released Before Trial"
label var bail_met "Released in 3 Days"
label var prior_offender_ever "Prior offender"


rename statecode state



keep court felony_court casenumber id firstname lastname firstthree lastfour dob race gender postal state ///
male white black age missing* clean_* attorney_public judicialsection ///
judge*  bail_* baildow bailmonth bailyear bailjudge bailtype ///
charge_down case_* confinement days_prison days_to_disposition highest_* prior* any_* n_* ///
days_* arrestdate shift jfe_* fe_* jyf_* jyearid ///
male100 black100 age100 prior_offender_ever100 n_counts100 highest_charge_felony100 ///
any_drug100 any_violent100 any_property100



compress
save "$clean_data/clean_Miami_data", replace



* Calculate runtime of do-file
local end_time_clean = clock(c(current_date) + " " + c(current_time), "DMYhms")
display "End time clean: " %tcCCYY-NN-DD_HH:MM:SS `end_time_clean'

	* Calculate time difference in milliseconds
	local duration_ms = `end_time_clean' - `start_time_clean'

	* Convert milliseconds to hours (1 hour = 3,600,000 ms)
	local duration_hours = `duration_ms' / 3600000

	* Display the result
	display "Duration clean: " `duration_hours' " hours"


log close



